/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author Alessio
 */
public class ConnectionManager
{
    private ConnectionManager()
    {
    }
    private static Connection conn;
    private static String IP;
    private static String MY_DRIVER =
            "com.mysql.jdbc.Driver";
    private static String MY_URL =
            "jdbc:mysql://";
    private static String LOGIN;
    private static String PASSWD;

    public static Connection getConnection() throws SQLException, ClassNotFoundException
    {
        
        if (conn == null) {
            Class.forName(MY_DRIVER);
            conn = DriverManager.getConnection(getURL(),
                    LOGIN, PASSWD);
        } else
            if (conn.isClosed())
                conn = DriverManager.getConnection(getURL(),
                        LOGIN, PASSWD);
            else
                if(conn.getAutoCommit() == false)
                    conn.rollback();
        return conn;
    }
    
    public static void setup(String ip, String login, String password){
        IP = ip;
        LOGIN = login;
        PASSWD = password;
    }
    
    private static String getURL(){
        return MY_URL + IP + "/updater";
    }
}